package cn.tedu.micro.order.service.impl;

import cn.tedu.micro.common.pojo.dto.OrderCreateParam;
import cn.tedu.micro.common.pojo.po.OrderInfoPO;
import cn.tedu.micro.common.pojo.protocol.JsonResult;
import cn.tedu.micro.common.pojo.vo.GunInfoVO;
import cn.tedu.micro.order.feign.DeviceClient;
import cn.tedu.micro.order.repository.OrderRepository;
import cn.tedu.micro.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private DeviceClient deviceClient;
    @Override
    public String createOrder(OrderCreateParam param) {
        // 判断枪状态
        JsonResult<GunInfoVO> result = deviceClient.getGunInfo(param.getGunId());
        GunInfoVO gunInfoVO = result.getData();
        if (gunInfoVO != null) {
            log.debug("远程调用查询结果：{}", gunInfoVO);
            if (gunInfoVO.getStatus() == 1) {
                OrderInfoPO orderInfoPO = new OrderInfoPO();
                BeanUtils.copyProperties(param, orderInfoPO);
                String billId = UUID.randomUUID().toString();
                orderInfoPO.setBillId(billId);
                orderRepository.createOrder(orderInfoPO);
                return billId;
            }
        }
        return null;
    }
}
